package com.study.www.controller;

import com.alibaba.fastjson.JSONObject;
import com.study.www.config.PageResult;
import com.study.www.entity.Dept;
import com.study.www.service.DeptJpaService;
import com.study.www.service.DeptTemplateService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.List;
import java.util.Optional;

/**
 * @author : Administrator.zhuyanpeng
 * @date : 2019/10/21 - 17:58
 */
@RequestMapping("/dept")
@RestController
public class DeptController {

    @Autowired
    DeptTemplateService deptTemplateService;
    @Autowired
    DeptJpaService deptJpaService;

    @PutMapping("")
    public String save(@RequestBody Dept dept) {
        Dept save = deptJpaService.save(dept);
        return save.getId();
    }

    @PostMapping("")
    public String change(@RequestBody Dept dept) {
        return deptJpaService.saveOrChange(dept);
    }

    @PostMapping("/template")
    public String  changeById(@RequestBody Dept dept) {
        deptTemplateService.changeDeptById(dept);
        return "ok";
    }

    @GetMapping("/id/{id}")
    public Dept findById(@PathVariable String id){
        return deptJpaService.findById(id);
    }


    @GetMapping({
                    "/page/{search}/{pageNum}/{pageSize}"
                    ,"/page/{search}"
                    ,"/page"
                    ,"/page/{pageNum}/{pageSize}"
                })
    public PageResult<Dept> queryPageMobile(@PathVariable String search,
                                  @PathVariable(required = false) Integer pageNum,
                                  @PathVariable(required = false) Integer pageSize) {
        if (!ObjectUtils.isEmpty(search)) {
            try {
                search =  URLDecoder.decode(search, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        pageNum = Optional.ofNullable(pageNum).orElseGet(() -> 1);
        pageSize = Optional.ofNullable(pageSize).orElseGet(() -> 10);
        return deptTemplateService.queryPage(search, pageNum, pageSize);
    }

    @GetMapping("/{search}")
    public String search(@PathVariable String search) {
        return JSONObject.toJSONString(deptTemplateService.findBySearch(search));
    }

    @GetMapping("/simple/{name}")
    public  PageResult<Dept> searchSimple(@PathVariable String name,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10")  Integer pageSize) {
        try {
            name =  URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return deptTemplateService.querySimplePage(name,pageNum,pageSize);
    }

    @GetMapping({"/salary/{startSalary}/{endSalary}", "/salary/{startSalary}"})
    public List<Dept> querySalary(@PathVariable BigDecimal startSalary,
                                  @PathVariable(required = false) BigDecimal endSalary) {
        if (ObjectUtils.isEmpty(endSalary)) {
            endSalary = startSalary;
            startSalary = new BigDecimal("0");
        }
        return deptTemplateService.queryBySalary(startSalary, endSalary);
    }

    @GetMapping({"/pagesSlary/{startSalary}/{endSalary}", "/salary/{startSalary}"})
    public PageResult<Dept> queryPageSalary(@PathVariable BigDecimal startSalary,
                                            @PathVariable(required = false) BigDecimal endSalary,
                                            @RequestParam(defaultValue = "1") Integer pageNum,
                                            @RequestParam(defaultValue = "10")  Integer pageSize) {
        if (ObjectUtils.isEmpty(endSalary)) {
            endSalary = startSalary;
            startSalary = new BigDecimal("0");
        }
        return deptTemplateService.queryPageSalary(startSalary, endSalary,pageNum,pageSize);
    }

    @DeleteMapping("/name/{name}")
    public String removeByName(@PathVariable String name){
        try {
            name =  URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return deptTemplateService.removeByName(name);
    }

    @GetMapping("/name/{name}")
    public List<Dept> queryByName(@PathVariable String name){
        try {
            name =  URLDecoder.decode(name, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return deptJpaService.queryByName(name);
    }
}
